package com.levi.juc.queue;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * 同步队列  没有容量
 * 往里面添加一个元素，只有等待元素被取出来之后才能再次往里面添加元素
 */
public class SynchronousQueueTest {
    public static void main(String[] args) throws InterruptedException {
        SynchronousQueue<String> queue = new SynchronousQueue<>();
        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getId()+"放入的值是:a");
                queue.put("a");
                System.out.println(Thread.currentThread().getId()+"放入的值是:b");
                queue.put("b");
                System.out.println(Thread.currentThread().getId()+"放入的值是:c");
                queue.put("c");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getId()+"取出的值是:"+queue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getId()+"取出的值是:"+queue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getId()+"取出的值是:"+queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        }



    }

